package com.benefm.multipar.pad.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.View;

public class DashboardView extends View {
    // 定义变量
    private Paint backgroundPaint;
    private Paint scalePaint;
    private Paint scaleValuePaint;
    private Paint pointerPaint;
    private Paint valuePaint;
    private Paint titlePaint;

    private RectF ovalRect;
    private int width, height;
    private float radius;
    private float startAngle = 130f;
    private float sweepAngle = 280f;
    private float maxValue = 100f;

    // 可配置项
    private float currentValue = 82f;
    private String title = "OXYGEN";
    private String unit = "%";
    private int warningColor = Color.RED;
    private int normalColor = Color.WHITE;
    private int criticalThreshold = 70;

    // 布局变量
    private float centerX, centerY;

    public DashboardView(Context context) {
        super(context);
        init();
    }

    public DashboardView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        // 背景弧线画笔
        backgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        backgroundPaint.setStyle(Paint.Style.STROKE);
        backgroundPaint.setStrokeWidth(15f);
        backgroundPaint.setColor(Color.parseColor("#2C3E50"));
        backgroundPaint.setStrokeCap(Paint.Cap.ROUND);

        // 刻度线画笔
        scalePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        scalePaint.setStrokeWidth(3f);
        scalePaint.setColor(Color.WHITE);
        scalePaint.setStrokeCap(Paint.Cap.ROUND);

        // 刻度值画笔
        scaleValuePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        scaleValuePaint.setTextSize(25f);
        scaleValuePaint.setColor(Color.WHITE);
        scaleValuePaint.setTextAlign(Paint.Align.CENTER);

        // 指针画笔
        pointerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        pointerPaint.setStrokeWidth(5f);
        pointerPaint.setColor(Color.RED);

        // 主数值画笔
        valuePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        valuePaint.setTextSize(55f);
        valuePaint.setColor(Color.WHITE);
        valuePaint.setTypeface(Typeface.DEFAULT_BOLD);
        valuePaint.setTextAlign(Paint.Align.CENTER);

        // 标题画笔
        titlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        titlePaint.setTextSize(30f);
        titlePaint.setColor(Color.LTGRAY);
        titlePaint.setTextAlign(Paint.Align.CENTER);

        ovalRect = new RectF();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        width = w;
        height = h;

        float padding = 30f;
        radius = Math.min(width, height) * 0.4f;
        centerX = width / 2f;
        centerY = height / 2f;

        // 计算弧形的矩形区域
        float ovalRadius = radius;
        ovalRect.set(
                centerX - ovalRadius,
                centerY - ovalRadius,
                centerX + ovalRadius,
                centerY + ovalRadius
        );
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // 1. 绘制背景弧线
        drawBackgroundArc(canvas);

        // 2. 绘制刻度线和刻度值
        drawScales(canvas);

        // 3. 绘制警告区域
        drawCriticalZone(canvas);

        // 4. 绘制指针
        drawPointer(canvas);

        // 5. 绘制主数值
        drawValue(canvas);

        // 6. 绘制标题
        drawTitle(canvas);
    }

    private void drawBackgroundArc(Canvas canvas) {
        canvas.drawArc(ovalRect, startAngle, sweepAngle, false, backgroundPaint);
    }

    private void drawScales(Canvas canvas) {
        int numMajorScales = 5; // 主刻度数量
        float scaleLength = radius * 0.1f;

        for (int i = 0; i <= numMajorScales; i++) {
            float value = i * (maxValue / numMajorScales);
            float angle = startAngle + (sweepAngle * i / numMajorScales);

            // 计算位置
            float angleRad = (float) Math.toRadians(angle);
            float startX = (float) (centerX + (radius) * Math.cos(angleRad));
            float startY = (float) (centerY + (radius) * Math.sin(angleRad));
            float endX = (float) (centerX + (radius - scaleLength) * Math.cos(angleRad));
            float endY = (float) (centerY + (radius - scaleLength) * Math.sin(angleRad));

            // 绘制刻度线
            canvas.drawLine(startX, startY, endX, endY, scalePaint);

            // 绘制刻度值
            float valueRadius = radius - scaleLength - 25f;
            float valueX = (float) (centerX + valueRadius * Math.cos(angleRad));
            float valueY = (float) (centerY + valueRadius * Math.sin(angleRad)) + 10;

            canvas.drawText(String.valueOf((int) value), valueX, valueY, scaleValuePaint);
        }

        // 添加小刻度
        int numMinorScales = numMajorScales * 4; // 小刻度数量
        float minorScaleLength = radius * 0.05f;

        for (int i = 0; i < numMinorScales; i++) {
            float angle = startAngle + (sweepAngle * i / numMinorScales);
            float angleRad = (float) Math.toRadians(angle);

            // 跳过主刻度位置
            if (i % 4 != 0) {
                float startX = (float) (centerX + radius * Math.cos(angleRad));
                float startY = (float) (centerY + radius * Math.sin(angleRad));
                float endX = (float) (centerX + (radius - minorScaleLength) * Math.cos(angleRad));
                float endY = (float) (centerY + (radius - minorScaleLength) * Math.sin(angleRad));

                canvas.drawLine(startX, startY, endX, endY, scalePaint);
            }
        }
    }

    private void drawCriticalZone(Canvas canvas) {
        Paint criticalPaint = new Paint(backgroundPaint);
        criticalPaint.setColor(warningColor);

        float criticalAngle = startAngle;
        float criticalSweep = sweepAngle * criticalThreshold / maxValue;

        canvas.drawArc(ovalRect, criticalAngle, criticalSweep, false, criticalPaint);
    }

    private void drawPointer(Canvas canvas) {
        float valueAngle = startAngle + sweepAngle * (currentValue / maxValue);
        float angleRad = (float) Math.toRadians(valueAngle);

        // 指针线
        float endX = (float) (centerX + (radius * 0.8f) * Math.cos(angleRad));
        float endY = (float) (centerY + (radius * 0.8f) * Math.sin(angleRad));
        canvas.drawLine(centerX, centerY, endX, endY, pointerPaint);

        // 指针点
        Paint centerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        centerPaint.setColor(warningColor);
        centerPaint.setStyle(Paint.Style.FILL);
        canvas.drawCircle(centerX, centerY, 8f, centerPaint);
    }

    private void drawValue(Canvas canvas) {
        // 显示主数值
        float valueY = centerY;
        canvas.drawText(String.format("%.0f", currentValue), centerX, valueY, valuePaint);

        // 显示单位
        float unitY = centerY + valuePaint.getTextSize() + 5;
        canvas.drawText(unit, centerX, unitY, titlePaint);
    }

    private void drawTitle(Canvas canvas) {
        float titleY = centerY + radius * 0.5f;
        canvas.drawText(title, centerX, titleY, titlePaint);
    }

    // 设置当前值
    public void setCurrentValue(float value) {
        if (value < 0) value = 0;
        if (value > maxValue) value = maxValue;
        this.currentValue = value;
        invalidate();
    }

    // 设置标题
    public void setTitle(String title) {
        this.title = title;
        invalidate();
    }

    // 设置单位
    public void setUnit(String unit) {
        this.unit = unit;
        invalidate();
    }

    // 设置警告阈值
    public void setCriticalThreshold(int threshold) {
        this.criticalThreshold = threshold;
        invalidate();
    }

    // 设置颜色
    public void setColors(int normalColor, int warningColor) {
        this.normalColor = normalColor;
        this.warningColor = warningColor;
        invalidate();
    }
}